package com.situ.personal.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.personal.dao.DepartmentDao;
import com.situ.personal.entity.Department;
import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.User;
import com.situ.personal.service.DepartmentService;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet{
	
	DepartmentService departmentService = new DepartmentService();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String departmentInfo = req.getParameter("method");
		if (departmentInfo==null || "".equals(departmentInfo)) {
			departmentInfo="insertDepartmentInfo";
		}
		switch (departmentInfo) {
		case "insertDepartmentInfo":
			insertDepartmentInfo(req,resp);
			break;
		case "updateById":
			updateById(req,resp);
			break;
		case "findByPage":
			findByPage(req,resp);
			break;
		case "deleteById":
			deleteById(req,resp);
			break;
		case "insertDepartment":
			insertDepartment(req,resp);
			break;
		case "selectById":
			selectById(req,resp);
			break;
		case "updateDepartmentInfo":
			updateDepartmentInfo(req,resp);
			break;

		default:
			break;
		}
		
	}//service

	private void updateDepartmentInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		
		boolean isUpdate = departmentService.updateDepartmentInfo(Integer.parseInt(id), name);
		if (isUpdate) {
			System.out.println("Department部门信息修改成功");
		}else {
			System.out.println("Department部门信息修改失败");
		}
		
		resp.sendRedirect(req.getContextPath()+"/department?method=findByPage");
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String departmentId = req.getParameter("id");
		
		Department department = departmentService.selectById(Integer.parseInt(departmentId));
		
		req.setAttribute("department", department);
		req.getRequestDispatcher("/department_update.jsp").forward(req, resp);
	}

	private void insertDepartment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		String DepartmentName = req.getParameter("name");
		
		boolean isInsert = departmentService.insertDepartment(DepartmentName);
		if (isInsert) {
			System.out.println("Department部门信息插入成功");
		}else {
			System.out.println("Department部门信息插入失败");
		}
		
		resp.sendRedirect(req.getContextPath()+"/department?method=findByPage");
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int id = Integer.parseInt(req.getParameter("id"));
		
		boolean isDelete = departmentService.deleteById(id);
		if (isDelete) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
		
		resp.sendRedirect(req.getContextPath()+"/department?method=findByPage");
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo==null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize==null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageInfo pageInfo = (PageInfo)departmentService.findByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
		
		req.setAttribute("DepartmentPageInfo", pageInfo);
		req.getRequestDispatcher("/department_page_list.jsp").forward(req, resp);
		
	}

	private void updateById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user = (User) req.getAttribute("user");
		
		List<Department> departmentList = departmentService.findAll();
		System.out.println("DepartmentServlet/updateById:"+departmentList);
		
		//req.setAttribute("departmentList", departmentList);  // 把 departmentList 部门表 信息拿到  jsp里部门信息下拉框  可以用遍历了
		req.setAttribute("user", user); // 把根据 id   拿到的user的信息  传过去     修改框里显示的值
		req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
		
	} //updateById

	private void insertDepartmentInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// 这聊获取一个list集合     把每一个部门的名字 编号   ，当成list的一个元素
		List<Department> list = departmentService.findAll();
		System.out.println(list);
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_insert.jsp").forward(req, resp);
		
	}//insertDepartmentInfo
	

	
}
